package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.ThDirectSupplyGoods;
import com.ltmall.api.dao.IDirectSupplyGoodsDAO;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DirectSupplyGoodsDAO extends HibernateDaoSupport implements IDirectSupplyGoodsDAO {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<ThDirectSupplyGoods> getGoodsByDirectSupply(int directSupplyId, String directType) {
        List list = new ArrayList<ThDirectSupplyGoods>();
        if (StringUtils.isBlank(directType)) {
            list = this.getHibernateTemplate()
                .find("from ThDirectSupplyGoods WHERE directSupplyId=? ",
                    new Object[] { directSupplyId });
        } else {
            list = this.getHibernateTemplate().find(
                "from ThDirectSupplyGoods WHERE directSupplyId=? and directType=? ",
                new Object[] { directSupplyId, directType });
        }

        if (list.size() > 0) {
            return list;
        }
        return null;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<ThDirectSupplyGoods> getGoodsByDirectSupplyAndGoodId(int directSupplyId, int goodId) {
        List list = this.getHibernateTemplate().find(
            "from ThDirectSupplyGoods WHERE directSupplyId=? and goodId=? ",
            new Object[] { directSupplyId, goodId });
        if (list.size() > 0) {
            return (List<ThDirectSupplyGoods>) list;
        }
        return null;
    }

    @Override
    public boolean save(ThDirectSupplyGoods info) {
        info.setGmtCreate(new Date());
        info.setGmtModified(new Date());
        info.setStatus("0");
        try {
            getHibernateTemplate().persist(info);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteByGoodId(int goodId) {
        String hql = "delete ThDirectSupplyGoods where goodId=? and directType='1' ";
        try {
            Query query = getSession().createQuery(hql);
            query.setParameter(0, goodId);
            int temp = query.executeUpdate();
            if (temp >= 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delete(int id) {
        String hql = "delete ThDirectSupplyGoods where goodId=? ";
        try {
            Query query = getSession().createQuery(hql);
            query.setParameter(0, id);
            int temp = query.executeUpdate();
            if (temp >= 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public int getUserGroupBuyingAmount(final int directSupplyId, final int userId) {
        final String SQL = "SELECT sum(foc.AMOUNT) FROM flavor_order_content foc LEFT join FLAVOR_ORDER fo on foc.ORDER_ID = fo.ORDER_ID"
                           + " WHERE  fo.user_id=? AND foc.ACTIVITY_ID=? AND foc.ACTIVITY_NAME='ThGroupBuyingRules' AND foc.ACTIVITY_SUCCESS='1'";

        List<String> list = this.getHibernateTemplate().execute(new HibernateCallback<List>() {
            public List doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery(SQL);
                query.setParameter(0, userId);
                query.setParameter(1, directSupplyId);
                return query.list();
            }
        });
        if (list.size() == 0) {
            return 0;
        } else {
            Object obj = list.get(0);
            if (obj == null) {
                return 0;
            } else {
                return Integer.parseInt(obj.toString());
            }
        }
    }

}
